package org.jeecg.modules.demo.crm.service.impl;

import org.checkerframework.checker.units.qual.A;
import org.jeecg.modules.demo.crm.entity.CInvoiceApply;
import org.jeecg.modules.demo.crm.entity.CInvoiceApplySplit;
import org.jeecg.modules.demo.crm.entity.CPaymentInvoice;
import org.jeecg.modules.demo.crm.mapper.CInvoiceApplyMapper;
import org.jeecg.modules.demo.crm.mapper.CInvoiceApplySplitMapper;
import org.jeecg.modules.demo.crm.service.ICInvoiceApplyService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;

import java.math.BigDecimal;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

/**
 * @Description: 开票任务表
 * @Author: jeecg-boot
 * @Date:   2025-06-13
 * @Version: V1.0
 */
@Service
public class CInvoiceApplyServiceImpl extends ServiceImpl<CInvoiceApplyMapper, CInvoiceApply> implements ICInvoiceApplyService {

    @Autowired
    CInvoiceApplyMapper cInvoiceApplyMapper;

    @Autowired
    CInvoiceApplySplitMapper cInvoiceApplySplitMapper;

    @Override
    public void add(CInvoiceApply cInvoiceApply) {
        String invoiceNumber = cInvoiceApply.getInvoiceNumber();
        //判断是否存在重复
        CInvoiceApply invoiceApply = cInvoiceApplyMapper.findByInvoiceNumber(invoiceNumber);
        if(invoiceApply != null){
            throw new RuntimeException("发票号重复");
        }
        cInvoiceApplyMapper.insert(cInvoiceApply);
    }

    @Override
    public void edit(CInvoiceApply cInvoiceApply) {
        String invoiceNumber = cInvoiceApply.getInvoiceNumber();
        //判断是否存在重复
        CInvoiceApply invoiceApply = cInvoiceApplyMapper.findByInvoiceNumber(invoiceNumber);
        if(invoiceApply != null && !invoiceApply.getId().equals(cInvoiceApply.getId())){
            throw new RuntimeException("发票号重复");
        }
        cInvoiceApplyMapper.updateById(cInvoiceApply);
    }

    @Override
    public List<CInvoiceApply> getLoadinvoiceAppleSplit(String projectId,  String invoiceAppleId) {

        if(invoiceAppleId == null || "".equals(invoiceAppleId)){
            invoiceAppleId ="";
        }
        //查询可以拆票的数据
        List<CInvoiceApply> list = cInvoiceApplyMapper.findiInvoiceAppSplit();
        List<CInvoiceApply> filteredList = list.stream()
                .filter(invoice ->(projectId == null || invoice.getProjectId().equals(projectId)))
                .collect(Collectors.toList());

        //查询 拆分开票记录
        String finalInvoiceAppleId = invoiceAppleId;
        filteredList.forEach(invoice -> {
            //此开票记录关联的金额
            BigDecimal splitAmount = cInvoiceApplySplitMapper.findbyInvoiceAndinvoIceApplyId(invoice.getId(), finalInvoiceAppleId);
            //未关联的金额
            BigDecimal splitAmountNot = cInvoiceApplySplitMapper.findbyInvoiceNot(invoice.getId(), finalInvoiceAppleId);
            if( invoice.getTotalAmountWithTax() == null){
                invoice.setTotalAmountWithTax(BigDecimal.ZERO);
            }
            BigDecimal highestSplitAmount = invoice.getTotalAmountWithTax().subtract(splitAmountNot);
            invoice.setSplitAmount(splitAmount);
            //最高
            invoice.setHighestSplitAmount(highestSplitAmount);
            //不在本单分配金额
            invoice.setSplitAmountNot(splitAmountNot);
        });

        return filteredList;
    }
}
